package com.efounder.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.core.xml.StubObject;
import com.efounder.adapter.BaseDelegateAdapter;
import com.efounder.adapter.BaseDelegateAdapterHelper;
import com.efounder.bean.BaseItem;
import com.efounder.bean.ItemList;
import com.efounder.chat.event.RightTopMenuClickEvent;
import com.efounder.chat.model.TabMenuClickEvent;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.http.EFHttpRequest;
import com.efounder.mobilemall.R;
import com.efounder.utils.MallRequestUtil;
import com.efounder.view.pop.MallPopManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_BANNER;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_BANNER_ITEM;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_GRID;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_GRID_SINGLE_MARK;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_LIST_DAILY_SELECT;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_ONE_PLUS_N_DAILY_NEW;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_RECOMMEND_ITEM;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_RECOMMEND_RECYCLER;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_RECYCLER;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_RECYCLER_WITH_BRAND;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_RECYCLER_WITH_PRICE;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_SNATCH_GOOD_ITEM;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_TIME_TO_BUY_ITEM;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE_TWO_SPAN_GRID;
import static com.efounder.adapter.BaseDelegateAdapterHelper.VIEW_TYPE__GRID_TWO;
import static com.efounder.utils.MallRequestUtil.HOME_URL;


/**
 * 商城首页（推荐页面）
 * Created by will on 18-1-5.
 *
 * @author will
 */

public class MallRecommendFragment extends BaseFragment {

    private String TAG = this.getClass().getSimpleName();

    private boolean hasMore = false;//加载更多
    private DelegateAdapter delegateAdapter;
    private BaseDelegateAdapterHelper baseDelegateAdapterHelper;
    VirtualLayoutManager layoutManager;
    private LinearLayout llNetworkErrorView;
    private BaseDelegateAdapter floatAdapter;

    public MallRecommendFragment() {
    }

    private View view;
    private SmartRefreshLayout refreshView;
    private RecyclerView mRecyclerView;
    private List<DelegateAdapter.Adapter> mAdapters; //存放各个模块的适配器
    private List<ItemList> mData = new ArrayList<>();
    private int pageSize = 10;
    private int num = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_recommend, container, false);
        ImageView ivBack = (ImageView) view.findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvTitle.setText(R.string.mail_mall);
        if (!isAlone()) {
            RelativeLayout titleLayout = view.findViewById(R.id.title);
            titleLayout.setVisibility(View.GONE);
        }

        refreshView = (SmartRefreshLayout) view.findViewById(R.id.xRefreshView);
        refreshView.setEnableLoadMore(false);
        refreshView.setEnableRefresh(true);
        llNetworkErrorView = (LinearLayout) view.findViewById(R.id.ll_network_error);
        TextView tvReload = (TextView) llNetworkErrorView.findViewById(R.id.tv_network_reload);
        tvReload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData(false);
            }
        });
        initView();
        mData.clear();
        initData(false);
        refreshView.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                initData(true);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData(false);
            }
        });
        refreshView.autoRefresh();
//        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                if (layoutManager.findFirstCompletelyVisibleItemPosition() == 1) {
//                    refreshView.autoRefresh();
//                }
//            }
//        });
        EventBus.getDefault().register(this);
        return view;
    }

    private void initView() {
        mAdapters = new ArrayList<>();

        mRecyclerView = (RecyclerView) view.findViewById(R.id.stickyContentView);
        //初始化
        layoutManager = new VirtualLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);
        refreshView.setNestedScrollingEnabled(true);
        //设置回收复用池大小，（如果一屏内相同类型的 View 个数比较多，需要设置一个合适的大小，防止来回滚动时重新创建 View）：
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        mRecyclerView.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 1);
        viewPool.setMaxRecycledViews(1, 10);
        viewPool.setMaxRecycledViews(2, 10);
        viewPool.setMaxRecycledViews(3, 10);
        viewPool.setMaxRecycledViews(4, 10);
        viewPool.setMaxRecycledViews(5, 10);
        viewPool.setMaxRecycledViews(6, 10);
        viewPool.setMaxRecycledViews(7, 10);
        viewPool.setMaxRecycledViews(8, 10);
        viewPool.setMaxRecycledViews(9, 10);
        viewPool.setMaxRecycledViews(10, 10);
        viewPool.setMaxRecycledViews(11, 10);
        viewPool.setMaxRecycledViews(12, 10);
        viewPool.setMaxRecycledViews(13, 10);
        viewPool.setMaxRecycledViews(14, 10);
        viewPool.setMaxRecycledViews(15, 5);
        viewPool.setMaxRecycledViews(16, 5);

        delegateAdapter = new DelegateAdapter(layoutManager, false);
        mRecyclerView.setAdapter(delegateAdapter);

        baseDelegateAdapterHelper = new BaseDelegateAdapterHelper(getActivity());
        floatAdapter = baseDelegateAdapterHelper.getFloatAdapter();


//        for (ItemList itemList : mData) {
//            switch (itemList.getViewType()){
//                case VIEW_TYPE_BANNER:
//                    mAdapters.add(baseDelegateAdapterHelper.getFloatAdapter());
//                    mAdapters.add(baseDelegateAdapterHelper.getBannerAdapter(itemList));
//                    break;
//                case VIEW_TYPE_BANNER_ITEM:
//                    mAdapters.add(baseDelegateAdapterHelper.getBannerItemAdapter(itemList));
//                    break;
//                case VIEW_TYPE_GRID:
////                case VIEW_TYPE_GRID_FOUR:
//                    mAdapters.add(baseDelegateAdapterHelper.getGridAdapter(itemList));
//                    break;
//                case VIEW_TYPE__GRID_TWO:
//                    mAdapters.add(baseDelegateAdapterHelper.getGridTwoAdapter(itemList));
//                    break;
//                case VIEW_TYPE_GRID_SINGLE_MARK:
//                    mAdapters.add(baseDelegateAdapterHelper.getSingleMarkAdapter(itemList));
//                    break;
//                case VIEW_TYPE_RECYCLER:
//                    mAdapters.add(baseDelegateAdapterHelper.getScrollAdapter(itemList));
//                    break;
//                case VIEW_TYPE_RECYCLER_WITH_BRAND:
//                    mAdapters.add(baseDelegateAdapterHelper.getScrollWithBrandAdapter(itemList));
//                    break;
//                case VIEW_TYPE_ONE_PLUS_N_DAILY_NEW:
//                    mAdapters.add(baseDelegateAdapterHelper.getDailyNewGridAdapter(itemList));
//                    break;
//                case VIEW_TYPE_LIST_DAILY_SELECT:
//                    mAdapters.add(baseDelegateAdapterHelper.getDailySelectListAdapter(itemList));
//                    break;
//                case VIEW_TYPE_RECOMMEND_ITEM:
//                    mAdapters.add(baseDelegateAdapterHelper.getRecommendItemAdapter(itemList));
//                    break;
//                case VIEW_TYPE_RECYCLER_WITH_PRICE:
//                    mAdapters.add(baseDelegateAdapterHelper.getScrollWithPriceAdapter(itemList));
//                    break;
//                case VIEW_TYPE_RECOMMEND_RECYCLER:
//                    mAdapters.add(baseDelegateAdapterHelper.getScrollWithRecommendAdapter(itemList));
//                    break;
//                default:
//                    break;
//            }
//        }
//        //设置适配器
//        delegateAdapter.setAdapters(mAdapters);
    }

    private void initData(final boolean hasMore) {
        if (!hasMore) {//刷新
            num = 0;
        } else {
            num += pageSize;
        }
        final HashMap<String, String> map = new HashMap<>();
        map.put("userId", EnvironmentVariable.getUserName());
        map.put("pageSize", pageSize + "");
        map.put("pageNo", num + "");
        MallRequestUtil.getRequest(TAG, HOME_URL, map, new MallRequestUtil.MallRequestCallback() {
            @Override
            public void onSuccess(String response) {
                parseResult(response, hasMore);
//                delegateAdapter.notifyDataSetChanged();
                for (ItemList itemList : mData) {
                    switch (itemList.getViewType()) {
                        case VIEW_TYPE_BANNER:
                            mAdapters.add(baseDelegateAdapterHelper.getBannerAdapter(itemList));
                            break;
                        case VIEW_TYPE_BANNER_ITEM:
                            mAdapters.add(baseDelegateAdapterHelper.getBannerItemAdapter(itemList));
                            break;
                        case VIEW_TYPE_GRID:
//                case VIEW_TYPE_GRID_FOUR:
                            mAdapters.add(baseDelegateAdapterHelper.getGridAdapter(itemList));
                            break;
                        case VIEW_TYPE__GRID_TWO:
                            mAdapters.add(baseDelegateAdapterHelper.getGridTwoAdapter(itemList));
                            break;
                        case VIEW_TYPE_GRID_SINGLE_MARK:
                            mAdapters.add(baseDelegateAdapterHelper.getSingleMarkAdapter(itemList));
                            break;
                        case VIEW_TYPE_RECYCLER:
                            mAdapters.add(baseDelegateAdapterHelper.getScrollAdapter(itemList));
                            break;
                        case VIEW_TYPE_RECYCLER_WITH_BRAND:
                            mAdapters.add(baseDelegateAdapterHelper.getScrollWithBrandAdapter(itemList));
                            break;
                        case VIEW_TYPE_ONE_PLUS_N_DAILY_NEW:
                            mAdapters.add(baseDelegateAdapterHelper.getDailyNewGridAdapter(itemList));
                            break;
                        case VIEW_TYPE_LIST_DAILY_SELECT:
                            mAdapters.add(baseDelegateAdapterHelper.getDailySelectListAdapter(itemList));
                            break;
                        case VIEW_TYPE_RECOMMEND_ITEM:
                            mAdapters.add(baseDelegateAdapterHelper.getRecommendItemAdapter(itemList));
                            break;
                        case VIEW_TYPE_SNATCH_GOOD_ITEM:
                            mAdapters.add(baseDelegateAdapterHelper.getSnatchGoodItemAdapter(itemList));
                            break;
                        case VIEW_TYPE_TIME_TO_BUY_ITEM:
                            mAdapters.add(baseDelegateAdapterHelper.getTimeToBuyItemAdapter(itemList));
                            break;
                        case VIEW_TYPE_RECYCLER_WITH_PRICE:
                            mAdapters.add(baseDelegateAdapterHelper.getScrollWithPriceAdapter(itemList));
                            break;
                        case VIEW_TYPE_RECOMMEND_RECYCLER:
                            mAdapters.add(baseDelegateAdapterHelper.getScrollWithRecommendAdapter(itemList));
                            break;
                        case VIEW_TYPE_TWO_SPAN_GRID://两列的类型
                            mAdapters.add(baseDelegateAdapterHelper.getTwoSpanGridAdapter(itemList));
                            break;
                        default:
                            break;
                    }
                }
                mAdapters.add(floatAdapter);
                mAdapters.add(baseDelegateAdapterHelper.getDividerAdapter());
                //设置适配器
                delegateAdapter.setAdapters(mAdapters);
                delegateAdapter.notifyDataSetChanged();
                refreshView.finishRefresh();
                refreshView.finishLoadMore();
            }

            @Override
            public void onFail(String error) {
                llNetworkErrorView.setVisibility(View.VISIBLE);
                refreshView.finishRefresh();
                refreshView.finishLoadMore();
            }
        });

    }


    private void parseResult(String result, boolean hasMore) {
        if (!hasMore) {
            mData.clear();
            mAdapters.clear();
        }


//        mAdapters.add(baseDelegateAdapterHelper.getSeckillItem(null));
//        mAdapters.add(baseDelegateAdapterHelper.getOneYuanToBuyItem(null));
        try {
            JSONObject resultObject = new JSONObject(result);
            if (resultObject.getString("result").equals("success")) {
                llNetworkErrorView.setVisibility(View.GONE);

                JSONArray homeData = resultObject.getJSONArray("home");
                for (int i = 0; i < homeData.length(); i++) {
                    ItemList itemList = new ItemList();
                    JSONObject itemListObject = homeData.getJSONObject(i);
                    String itemType = itemListObject.getString("viewCode");
                    if (itemType.equals("banners")) {//banner
                        itemList.setViewType(VIEW_TYPE_BANNER);
                        JSONArray items = itemListObject.getJSONArray("bannerList");
                        if (items.length() > 0) {
                            List<BaseItem> baseItems = new ArrayList<>();
                            for (int j = 0, n = items.length(); j < n; j++) {
                                BaseItem baseItem = new BaseItem();
                                JSONObject itemObject = items.getJSONObject(j);
                                baseItem.setImgUrl(itemObject.getString("imgUrl"));
                                baseItem.setIntent(itemObject.getString("intent"));
                                baseItem.setIntentType(itemObject.getString("intentType"));
                                String id = itemObject.optString("id");
                                baseItems.add(baseItem);
                            }
                            itemList.setItems(baseItems);
                        }

                    } else if (itemType.equals("groupView2")) {//横向滑动商品
                        itemList.setViewType(VIEW_TYPE_RECYCLER);
                        itemList.setImageUrl(itemListObject.getString("imgUrl"));
                        JSONArray items = itemListObject.getJSONArray("itemList");
                        if (items.length() > 0) {
                            List<BaseItem> baseItems = new ArrayList<>();
                            for (int j = 0, n = items.length(); j < n; j++) {
                                BaseItem baseItem = new BaseItem();
                                JSONObject itemObject = items.getJSONObject(j);
                                baseItem.setImgUrl(itemObject.getString("imgUrl"));
                                baseItem.setIntent(itemObject.getString("intent"));
                                baseItem.setIntentType(itemObject.getString("intentType"));
                                baseItems.add(baseItem);
                            }
                            itemList.setItems(baseItems);
                        }
                    } else if (itemType.equals("horizontalViewGroup")) {//横向多图
                        JSONArray items = itemListObject.getJSONArray("itemList");
                        itemList.setViewType(VIEW_TYPE_GRID);
                        List<BaseItem> baseItems = new ArrayList<>();
                        for (int j = 0, n = items.length(); j < n; j++) {
                            BaseItem baseItem = new BaseItem();
                            JSONObject itemObject = items.getJSONObject(j);
                            if (itemObject.has("imgUrl")) {
                                baseItem.setImgUrl(itemObject.getString("imgUrl"));
                            } else {
                                baseItem.setImgUrl(itemObject.getString("imageUrl"));
                            }
                            if (itemObject.has("intent")) {
                                baseItem.setIntent(itemObject.getString("intent"));
                                baseItem.setIntentType(itemObject.getString("intentType"));
                            }
                            baseItems.add(baseItem);
                        }
                        itemList.setItems(baseItems);
                    } else if (itemType.equals("020701")) {//两类的布局
                        JSONArray items = itemListObject.getJSONArray("itemList");
                        itemList.setViewType(VIEW_TYPE_TWO_SPAN_GRID);
                        List<BaseItem> baseItems = new ArrayList<>();
                        for (int j = 0; j < items.length(); j++) {
                            BaseItem baseItem = new BaseItem();
                            baseItem.setImgUrl(items.getJSONObject(j).getString("imgUrl"));
                            baseItem.setIntent(items.getJSONObject(j).getString("linkUrl"));
                            baseItem.setName(items.getJSONObject(j).getString("albumActivityName"));//价格
                            baseItem.setCurrentPrice(items.getJSONObject(j).getString("price"));
                            baseItem.setPriceUnit(items.getJSONObject(j).optString("priceUnit"));
                            baseItems.add(baseItem);
                        }
                        itemList.setItems(baseItems);
//                    } else if (itemType.equals("021201")) {
//                        JSONArray items = itemListObject.getJSONArray("itemList");
//                        itemList.setViewType(VIEW_TYPE_GRID_FOUR);
//                        List<BaseItem> baseItems = new ArrayList<>();
//                        for (int j = 0; j < items.length(); j++) {
//                            BaseItem baseItem = new BaseItem();
//                            baseItem.setImgUrl(items.getJSONObject(j).getString("imgUrl"));
//                            baseItem.setIntent(items.getJSONObject(j).getString("linkUrl"));
//                            baseItems.add(baseItem);
//                        }
//                        itemList.setItems(baseItems);
                    } else if (itemType.equals("020201")) {
                        itemList.setImageUrl(itemListObject.getString("imageUrl"));
                        itemList.setLinkUrl(itemListObject.getString("linkUrl"));
                        itemList.setViewType(VIEW_TYPE_GRID_SINGLE_MARK);
                    } else if (itemType.equals("010901")) {
                        JSONArray items = itemListObject.getJSONArray("itemList");
                        itemList.setViewType(VIEW_TYPE_BANNER_ITEM);
                        List<BaseItem> baseItems = new ArrayList<>();
                        for (int j = 0; j < items.length(); j++) {
                            BaseItem baseItem = new BaseItem();
                            baseItem.setImgUrl(items.getJSONObject(j).getString("imageUrl"));
                            baseItem.setIntent(items.getJSONObject(j).getString("linkUrl"));
                            baseItems.add(baseItem);
                        }
                        itemList.setItems(baseItems);
                    } else if (itemType.equals("groupView3")) {// 横向滑动带品牌
                        if (itemListObject.has("imgUrl")) {
                            itemList.setImageUrl(itemListObject.getString("imgUrl"));
                            JSONArray items = itemListObject.getJSONArray("itemList");
                            itemList.setViewType(VIEW_TYPE_RECYCLER_WITH_BRAND);
                            if (items.length() > 0) {
                                List<BaseItem> baseItems = new ArrayList<>();
                                for (int j = 0, n = items.length(); j < n; j++) {
                                    BaseItem baseItem = new BaseItem();
                                    JSONObject itemObject = items.getJSONObject(j);
                                    baseItem.setLogoUrl(itemObject.getString("albumActivityImgurl"));
                                    baseItem.setIconUrl(itemObject.getString("brandimg"));
                                    baseItem.setName(itemObject.getString("albumActivityName"));
                                    baseItem.setIntroduce(itemObject.getString("albumActivityIntro"));
                                    baseItem.setIntent(itemObject.getString("intent"));
                                    baseItem.setIntentType(itemObject.getString("intenttype"));
                                    baseItems.add(baseItem);
                                }
                                itemList.setItems(baseItems);
                            }
                        } else {
                            itemList.setViewType(22);
                        }
                    } else if (itemType.equals("groupView1")) {//横向滑动带价格
                        itemList.setImageUrl(itemListObject.getString("imgUrl"));
//                        itemList.setIntent(itemListObject.getString("linkUrl"));
                        JSONArray items = itemListObject.getJSONArray("itemList");
                        itemList.setViewType(VIEW_TYPE_RECYCLER_WITH_PRICE);
                        if (items.length() > 0) {
                            List<BaseItem> baseItems = new ArrayList<>();
                            for (int j = 0, n = items.length(); j < n; j++) {
                                BaseItem baseItem = new BaseItem();
                                JSONObject itemObject = items.getJSONObject(j);
                                baseItem.setTitle(itemObject.getString("title"));
                                baseItem.setImgUrl(itemObject.getString("imgUrl"));
                                if (itemObject.has("benefitPoint")) {
                                    baseItem.setSingleBenefitPoint(itemObject.getString("benefitPoint"));
                                }
                                baseItem.setItemId(itemObject.getString("goodsId"));
                                baseItem.setCurrentPrice(itemObject.getString("price"));
                                baseItem.setPriceUnit(itemObject.optString("priceUnit"));
                                baseItem.setIntent(itemObject.getString("intent"));
                                baseItem.setIntentType(itemObject.getString("intentType"));
                                baseItems.add(baseItem);
                            }
                            itemList.setItems(baseItems);
                        }
                    } else if (itemType.equals("groupView4")) {//榜单，横向滑动商品
                        itemList.setAlbumDesc(itemListObject.getString("albumDesc"));
                        itemList.setTitle(itemListObject.getString("title"));
                        itemList.setBuyerNum(2);//todo
                        itemList.setGoodsNum(12);//todo
                        itemList.setHasFavor(false);//todo
                        JSONArray items = itemListObject.getJSONArray("albumGoodsList");
                        itemList.setViewType(VIEW_TYPE_RECOMMEND_RECYCLER);
                        if (items.length() > 0) {
                            List<BaseItem> baseItems = new ArrayList<>();
                            for (int j = 0, n = items.length(); j < n; j++) {
                                BaseItem baseItem = new BaseItem();
                                JSONObject itemObject = items.getJSONObject(j);
                                baseItem.setImgUrl(itemObject.getString("imgUrl"));
                                baseItem.setItemId(itemObject.getString("goodsId"));
                                baseItems.add(baseItem);
                            }
                            itemList.setItems(baseItems);
                        }
                    } else if (itemType.equals("021101")) {
                        JSONArray items = itemListObject.getJSONArray("itemList");
                        itemList.setViewType(VIEW_TYPE_ONE_PLUS_N_DAILY_NEW);
                        List<BaseItem> baseItems = new ArrayList<>();
                        for (int j = 0; j < items.length(); j++) {
                            BaseItem baseItem = new BaseItem();
                            baseItem.setImgUrl(items.getJSONObject(j).getString("imageUrl"));
                            baseItem.setIntent(items.getJSONObject(j).getString("linkUrl"));
                            baseItems.add(baseItem);
                        }
                        itemList.setItems(baseItems);
                    } else if (itemType.equals("011401")) {
                        JSONArray items = itemListObject.getJSONArray("itemList");
                        itemList.setViewType(VIEW_TYPE_LIST_DAILY_SELECT);
                        itemList.setTitle(itemListObject.getJSONArray("guidanceViewList").getJSONObject(0).getString("title"));
                        List<BaseItem> baseItems = new ArrayList<>();
                        for (int j = 0; j < items.length(); j++) {
                            BaseItem baseItem = new BaseItem();
                            baseItem.setImgUrl(items.getJSONObject(j).getString("imageUrl"));
//                            baseItem.setItemId(items.getJSONObject(j).getInt("itemId"));
                            baseItem.setTitle(items.getJSONObject(j).getString("title"));
                            baseItem.setDesc(items.getJSONObject(j).getString("desc"));
                            baseItems.add(baseItem);
                        }
                        itemList.setItems(baseItems);
                    } else if (itemType.equals("goodItem")) {//单件推荐商品
                        itemList.setViewType(VIEW_TYPE_RECOMMEND_ITEM);
                        itemList.setExcellentComment("");//todo 后期加字段
                        itemList.setTitle(itemListObject.getString("title"));
                        itemList.setImageUrl(itemListObject.getString("imgUrl"));
                        itemList.setIntro(itemListObject.getString("introduce"));
                        itemList.setPrice(itemListObject.getString("currentPrice"));
                        itemList.setPriceUnit(itemListObject.optString("priceUnit"));
                        itemList.setIntent(itemListObject.getString("intent"));
                        itemList.setIntentType(itemListObject.getString("intentType"));
                    } else if (itemType.equals("snatchGoodItem")) {//夺宝商品
                        itemList.setViewType(VIEW_TYPE_SNATCH_GOOD_ITEM);
                        itemList.setExcellentComment("");//todo 后期加字段
                        itemList.setTitle(itemListObject.getString("title"));
                        itemList.setImageUrl(itemListObject.getString("imgUrl"));
                        itemList.setIntro(itemListObject.getString("introduce"));
                        itemList.setPrice(itemListObject.getString("currentPrice"));
                        itemList.setPriceUnit(itemListObject.optString("priceUnit"));
                        itemList.setIntent(itemListObject.getString("intent"));
                        itemList.setGoodMark(itemListObject.optString("mark"));
                        itemList.setIntentType(itemListObject.getString("intentType"));
                    } else if (itemType.equals("timeToBuyItem")) {//夺宝商品
                        itemList.setViewType(VIEW_TYPE_TIME_TO_BUY_ITEM);
                        itemList.setExcellentComment("");//todo 后期加字段
                        itemList.setTitle(itemListObject.getString("title"));
                        itemList.setImageUrl(itemListObject.getString("imgUrl"));
                        itemList.setIntro(itemListObject.getString("introduce"));
                        itemList.setPrice(itemListObject.getString("currentPrice"));
                        itemList.setPriceUnit(itemListObject.optString("priceUnit"));
                        itemList.setIntent(itemListObject.getString("intent"));
                        itemList.setGoodMark(itemListObject.optString("mark"));
                        itemList.setIntentType(itemListObject.getString("intentType"));
                    } else {
                        itemList.setViewType(22);
                    }
                    mData.add(itemList);
                }
            } else {
                llNetworkErrorView.setVisibility(View.VISIBLE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            refreshView.finishRefresh();
            refreshView.finishLoadMore();
            llNetworkErrorView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (baseDelegateAdapterHelper != null) {
            floatAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onStop() {
        super.onStop();
        EFHttpRequest.cancelRequest(TAG);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showPopMenu1(RightTopMenuClickEvent event) {
        StubObject stubObject = event.getStubObject();
        if (stubObject.getString("AndroidShow", "").equals("com.efounder.fragment.MallRecommendFragment")) {
            MallPopManager.showMineAndCartPop(getActivity(), event.getView());
        }
    }

    //TODO 处理tabmenu 点击(滚动到顶部)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public synchronized void onSolveEvent(TabMenuClickEvent event) {
        if (getArguments() != null) {
            StubObject stubObject = (StubObject) getArguments().getSerializable("stubObject");
            if (event.isChildPage(stubObject) && !isHidden()) {
                mRecyclerView.smoothScrollToPosition(0);
                refreshView.autoRefresh();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
